<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_mail_MailSmtp extends f_mail_MailMessage
{
	private static $instance;
	private $mailMime;
	private $smtp;
	private $host;
	private $port;
	private $timeout;
	private $keepalive = false;
	private $priority = 3; /*1 High 3 Normal 5 Low*/
	private $app_name = "SendNews ADVISA(c)";
	function __construct()
	{//just ...
	}
	
	function  __destruct()
	{
		$this->smtpClose();
	}
	
	public static function getInstance($new_instance = false)
	{
		if($new_instance)
		{
			return new self();
		}
		else if(self::$instance === null)
		{
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	public function setKeepAlive($kp)
	{
		$this->keepalive = $kp;
	}
	
	public function getPriority()
	{
		return $this->priority;
	}
	
	public function getAppName()
	{
		return $this->app_name;
	}
	
	public function getMailMime()
	{
		return $this->mailMime;
	}
	
	public function smtpConnect($host = "localhost", $port = 25,$timeout = 10)
	{
		$this->port = $port;
		$this->host = $host;
		$this->timeout = $timeout;
		
		if($this->smtp == NULL)
		{
			$this->smtp = new f_mail_Smtp();
		}
		
		if($this->smtp->Connect($this->host,$this->port,$this->timeout))
		{
			$this->smtp->Hello($this->host);
			return true;
		}
		else
		{
			$this->smtp->Reset();
			return false;
		}
		
	}
	
	public function smtpClose()
	{
		if($this->smtp != NULL)
		{
			if($this->smtp->Connected())
			{
				$this->smtp->Quit();
				$this->smtp->Close();
			}
		}
	}
	
	/*error :
	-1 not connected
	-2 missing senders
	-3 missing receiver

	-5 sender not accepted from smtp server
	-6 receiver not accepted from smtp server

	-10 data not accepted from smtp server
	*/
	public function smtpSend()
	{
		/*TO GET HEADER EVEN IF WE DIDN'T SENT THE MAIL*/
		$this->mailMime = f_mail_MailMime::getInstance($this);
		/*SMTP CONNEXION*/
		if(!$this->smtp->Connected())
		{
			return -1;	
		}
		/*SENDER VERIFICATION*/
		if($this->getSender() == "")
		{
			return -2;
		}
		else
		{
			if(!$this->smtp->Mail($this->sender))
			{
				return -5;
			}
		}
		/*RECEIVER VERIFICATION*/
		if($this->getReceiver() == "")
		{
			return -3;
		}
		else
		{
			if(!$this->smtp->Recipient($this->getReceiver()))
			{
				return -6;
			}
		}
		
		$data = $this->mailMime->getHeaders(true).$this->mailMime->getHTML();
		if(!$this->smtp->Data($data))
		{
			return -10;
		}
		if($this->keepalive == true)
		{
			$this->smtp->Reset();//so we can still use this connection
		}
		else
		{
			$this->smtpClose();
		}
		return 0;//everythings is ok
		
	}
	
	function getError()
	{
		return $this->smtp->getError();
	}
}
?>